package com.hlhj.carview.aty

import android.content.Intent
import android.net.Uri
import com.bumptech.glide.Glide
import com.hlhj.carview.BaseAty
import com.hlhj.carview.Constant
import com.hlhj.carview.Iview.AddEquipmentView
import com.hlhj.carview.Iview.UploadView
import com.hlhj.carview.R
import com.hlhj.carview.bean.MyBaseBean
import com.hlhj.carview.presenter.AddEquipmentPresenter
import com.hlhj.carview.presenter.UploadPresenter
import com.hlhj.carview.utils.MyGlideEngin
import com.hlhj.carview.utils.ToastUtil
import com.huantansheng.easyphotos.EasyPhotos
import kotlinx.android.synthetic.main.aty_addequipment2.*
import kotlinx.android.synthetic.main.lo_pubhead.*
import java.io.File

/**
 * Created by Never Fear   on 2018\12\24 0024.
Never More....
 */
class AddEquipmentAty : BaseAty<AddEquipmentView, AddEquipmentPresenter>(), AddEquipmentView, UploadView {
    private var uploadPresenter: UploadPresenter?=null
    private var url1=""
    private var url2=""
    private var url3=""

    override fun setUpResult(myBaseBean: MyBaseBean) {
        
    }

    override fun setUpResult(myBaseBean: MyBaseBean, tag: Int) {
        when(tag){
            TAKEPHOTO1->{
                url1=myBaseBean.info.toString()
            }
            TAKEPHOTO2->{
                url2=myBaseBean.info.toString()

            }
            TAKEPHOTO3->{
                url3=myBaseBean.info.toString()
            }
        }
    }

    override fun setRepair(myBaseBean: MyBaseBean) {
        ToastUtil.toast(myBaseBean.msg.toString(), this)
        setResult(-1)
        finish()
    }
    override fun setAddResult(myBaseBean: MyBaseBean) {

    }
    override fun initView() {
        tvTittle.text="设备保修"
    }

    override fun logicStart() {

    }
    private val TAKEPHOTO1: Int=1001
    private val TAKEPHOTO2: Int=1002
    private val TAKEPHOTO3: Int=1003
    override fun setListener() {
        img0.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.carview.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO1)
        }
        img1.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.carview.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO2)
        }
        img2.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.carview.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO3)
        }
        btCommit.setOnClickListener {
            if (etName.text.isEmpty()){
                ToastUtil.toast("请填写名称", this)
                return@setOnClickListener
            }
            if (etPhone.text.isEmpty()){
                ToastUtil.toast("请填写手机号码", this)
                return@setOnClickListener
            }
            if (etWechat.text.isEmpty()){
                ToastUtil.toast("请填写微信", this)
                return@setOnClickListener
            }
            if (etReason.text.isEmpty()){
                ToastUtil.toast("请填写理由", this)
                return@setOnClickListener
            }
            if (url1.isEmpty()||url2.isEmpty()||url3.isEmpty()){
                ToastUtil.toast("请补全图片", this)
                return@setOnClickListener
            }
            presenter?.repair(intent.getIntExtra("did",0),etName.text.toString(),etPhone.text.toString(),etWechat.text.toString(),etReason.text.toString()
            ,"${url1},${url2},${url3}",this)
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
        when(requestCode){
            TAKEPHOTO1->{
                uploadPresenter?.upload(File(pathList[0])!!,this@AddEquipmentAty,TAKEPHOTO1)
                Glide.with(this).load(pathList[0]).into(img0)
            }

            TAKEPHOTO2->{
                uploadPresenter?.upload(File(pathList[0]),this@AddEquipmentAty,TAKEPHOTO2)
                Glide.with(this).load(pathList[0]).into(img1)

            }
            TAKEPHOTO3->{
                uploadPresenter?.upload(File(pathList[0]),this@AddEquipmentAty,TAKEPHOTO3)
                Glide.with(this).load(pathList[0]).into(img2)

            }

        }
    }
    override fun bindView(): AddEquipmentView {
        return this
    }

    override fun createPresenter(): AddEquipmentPresenter {
        uploadPresenter= UploadPresenter()
        uploadPresenter?.let {
            it.createModule()
            it.bindView(this@AddEquipmentAty)
        }
        return AddEquipmentPresenter()
    }

    override fun getContentID(): Int {
        return R.layout.aty_addequipment2
    }
}